package mines.fila3.letstalk.adapters;

import mines.fila3.letstalk.R;
import mines.fila3.letstalk.data.ListMessages;
import mines.fila3.letstalk.data.MessageLetstalk;
import mines.fila3.letstalk.params.LetsTalkConst;
import mines.fila3.letstalk.tools.ThreadPreconditions;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * This class allows to create a specify adapter for the list of messages.
 * 
 * @author aminaud & qmahe
 * 
 */
public class MessagesAdapter extends BaseAdapter {

	private ListMessages listMessages;
	private final Context context;

	/**
	 * Constructor of this adapter.
	 * 
	 * @param context
	 */
	public MessagesAdapter(Context context) {
		this.context = context;
		listMessages = new ListMessages();
	}

	@Override
	public int getCount() {
		return listMessages.sizeView();
	}

	@Override
	public MessageLetstalk getItem(int position) {
		return listMessages.getViewItem(position);
	}

	@Override
	public long getItemId(int position) {
		return listMessages.getViewItemId(position);
	}

	/**
	 * This method allows to update the list of messages for the adapter.
	 * 
	 * @param messages
	 *              an array which contains all messages.
	 */
	public boolean updateData(String[] messages) {
		ThreadPreconditions.checkOnMainThread();
		if (listMessages.updateListMessages(messages)) {
			notifyDataSetChanged();
			return true;
		}
		return false;
	}

	/**
	 * @return the listMessages
	 */
	public ListMessages getListMessages() {
		return listMessages;
	}

	/**
	 * To modify the entire list with a new one.
	 * 
	 * @param listMessages
	 *              the listMessages to set
	 */
	public void setListMessages(ListMessages listMessages) {
		ThreadPreconditions.checkOnMainThread();
		this.listMessages = listMessages;
		notifyDataSetChanged();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.listview_item, parent, false);
		}

		TextView tvAuthor = MessagesViewHolder.get(convertView, R.id.item_author);
		TextView tvMessage = MessagesViewHolder.get(convertView, R.id.item_message);
		TextView tvNumber = MessagesViewHolder.get(convertView, R.id.item_number);
		LinearLayout layout = MessagesViewHolder.get(convertView, R.id.item_layout);

		MessageLetstalk message = getItem(position);
		tvAuthor.setText(message.getAuthor());
		tvMessage.setText(message.getContent());
		tvNumber.setText(position + "");

		String username = context.getSharedPreferences(LetsTalkConst.USER_CREDENTIALS, Context.MODE_PRIVATE)
				.getString(LetsTalkConst.KEY_USERNAME, LetsTalkConst.EMPTY_STRING);

		if (message.getAuthor().equals(username)) {
			tvAuthor.setTextColor(Color.BLUE);
			tvMessage.setTextColor(Color.BLUE);
			tvNumber.setTextColor(Color.BLUE);
		} else if (!message.isRead()) {
			tvAuthor.setTextColor(Color.RED);
			tvMessage.setTextColor(Color.RED);
			tvNumber.setTextColor(Color.RED);
		} else {
			tvAuthor.setTextColor(Color.BLACK);
			tvMessage.setTextColor(Color.BLACK);
			tvNumber.setTextColor(Color.BLACK);
		}

		return convertView;
	}

	/**
	 * This method allows to mark a message as read.
	 * 
	 * @param position
	 *              the position which corresponds to the message.
	 */
	public void setReadMessage(int position) {
		ThreadPreconditions.checkOnMainThread();
		if (listMessages.setReadMessage(position)) {
			notifyDataSetChanged();
		}
	}

	/**
	 * This method allows to mark all messages as read.
	 */
	public void setReadAllMessages() {
		ThreadPreconditions.checkOnMainThread();
		if (listMessages.setReadAllMessages())
			notifyDataSetChanged();
	}
}
